import * as step from '@ali/flow-step-toolkit'
import process from 'process'
import {installJDK, installMaven} from './install'

const ENV_NAME_JDK_VERSION: string = 'CI_RUNTIME_VERSION'
const ENV_NAME_MAVEN_VERSION: string = 'MAVEN_VERSION'
const ENV_NAME_JDK_INSTALL_PATH: string = 'INSTALL_JDK_PATH'
const ENV_NAME_MAVEN_INSTALL_PATH: string = 'INSTALL_MAVEN_PATH'

async function runStep(): Promise<void> {
    const inputJdkVersion: string | undefined =
        process.env[ENV_NAME_JDK_VERSION]
    const inputMavenVersion: string | undefined =
        process.env[ENV_NAME_MAVEN_VERSION]

    if (inputJdkVersion == undefined) {
        let errMsg = 'missing jdk version';
        throw new Error(errMsg)
    }

    if (inputMavenVersion == undefined) {
        let errMsg = 'missing maven version';
        throw new Error(errMsg)
    }


    const installJDKPath: string | undefined =
        process.env[ENV_NAME_JDK_INSTALL_PATH]
    const installMavenPath: string | undefined =
        process.env[ENV_NAME_MAVEN_INSTALL_PATH]
    await installJDK(inputJdkVersion, installJDKPath)
    await installMaven(inputMavenVersion, installMavenPath)

}

runStep()
    .then(function () {
        step.info('Run setupJava step successfully!')
    })
    .catch(function (err: Error) {
        step.error(err.message)
        process.exit(-1)
    })
